$(function(){
	/*表单提交*/
	$(".btn").click(function(){
		var page     = $("#page").val();
		var keywords = $("#keywords").val();
		var userName =$("#userName").val();
		var phone    =$("#phone").val();
		var realName =$("#realName").val();
		var position =$("#position").val();
		var desc     =$("#textarea").val();
		var adminId  =$("#adminId").val();
		var status   =$('input:radio[name="state"]:checked').attr("value");
		var sex      =$('input:radio[name="sex"]:checked').attr("value");
		var email = $("#email").val();
		var emailReg =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
     	var phoneReg=/^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/;	
		if(userName==''){
			show_err_msg('用户名还没填呢！');
			return false;
		}else if(/(\s)+/.test(userName)){
			show_err_msg('用户名不能包含空格！');
			return false;
		}else if(phone==''){
			show_err_msg('联系电话还没填呢！');
			return false;
		}else if(!phoneReg.test(phone)){
			show_err_msg('联系电话请输入正确格式！');
			return false;
		}else if(realName==''){
			show_err_msg('真实姓名还没填呢！');
			return false;
		}else if(position==''){
			show_err_msg('职位还没填呢！');
			return false;
		}else if(email!='' && !emailReg.test(email)){
			show_err_msg('邮箱格式有问题！');
			return false;
		}
		userInsert(userName,phone,realName,position,desc,status,sex,email,keywords,page,adminId);
	});	
});
function userInsert(userName,phone,realName,position,desc,status,sex,email,keywords,page,adminId){
	var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj;
            try{
                obj =  eval('(' + xmlhttp.responseText + ')');
            }catch(exception){
                obj="error";
            }
            if(obj == "error"){
                show_err_msg("服务链接出错！");    
            }else{
                if(obj.status == "succeed")
					show_msg(obj.info,"/user/?keywords="+keywords+"&page="+page);
				else if(obj.status == "fail")
					show_err_msg(obj.info);
	            }
	        }
        }
    xmlhttp.open("POST", "/ajaxInsert/userInsert/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("userName="+userName+"&phone="+phone+"&realName="+
	   			  realName+"&position="+position+"&desc="+desc+"&status="+status+"&sex="+sex+"&email="+email+"&adminId="+adminId);
}